<?php
namespace Admin\Controller;
use Common\Controller\BackendController;
class UpgradeController extends BackendController{

	private $_tbconfig = null;
    public function _initialize( ){
        parent::_initialize( );
        $this->_mod_setting = D("setting");
		$api_config = M('ItemsSite')->where(array('code' => 'itaoke'))->getField('config');
        $this->_tbconfig = unserialize($api_config);
    }

    public function index(){
        exit();
    }

    public function version(){
		if(!$this->_tbconfig['app_key']){$this->ajaxReturn(0, '请设置爱淘客开放平台appkey');}
		$tb_top = $this->_get_tb_top();
        $req = $tb_top->load_api('ItaokeUpgradeGetRequest');
        $req->setRelease(ITK_RELEASE);
		$req->setTime(date("y-m-d-h",time()));
        $resp = $tb_top->execute($req);
        $result = (array)$resp->result;

        if($result && $result['status'] == 1){
			if(class_exists("ZipArchive")){
				$durl = $result['url'];
				$newfname = date('YmdHis',time()).".zip";
				$width = 500;
				$file = fopen($durl,"rb");
				if($file){
					$filesize = -1;
					$headers = get_headers( $durl, 1 );
					if(!array_key_exists( "Content-Length", $headers ) ){
						$filesize = 0;
					}
					$filesize = $headers['Content-Length'];
					$total = $filesize;
					$pix = $width / $total;
					$newf = fopen( $newfname, "wb" );
					while ($newf && !feof($file)){
						$data = fread( $file, 8192 );
						$downlen += strlen( $data );
						fwrite($newf, $data, 8192 );
					}
					if($file){
						fclose($file);
					}
					if($newf){
						fclose($newf);
					}
				}else{
					$this->ajaxReturn( 0, "没有更新的文件-8" );
				}
				@ob_flush();
				@flush();
				$zip_filepath = $newfname;
				if(!is_file($zip_filepath)){
					$this->ajaxReturn( 0, "没有更新的文件-9" );
				}

				$zip = new \ZipArchive();
				$rs = $zip->open( $zip_filepath );
				if($rs!== TRUE){
					$this->ajaxReturn( 0, "更新失败" );
				}
				$destination = ".";
				$zip->extractTo($destination);
				$zip->close();
				if(file_exists($newfname)){
					unlink( $newfname );
				}

				if(file_exists('update.txt')){
					$this->_database_mod = new \Think\Model();
					$sql_str = file('update.txt');
					$sql_str = str_replace("\r", '', implode('', $sql_str));
					$ret = explode(";\n", $sql_str);
					$ret_count = count($ret);
					for ($i = 0; $i < $ret_count; $i++)
					{
						$ret[$i] = trim($ret[$i], " \r\n;"); 
						if (!empty($ret[$i]))
						{
							$this->_database_mod->query($ret[$i]);
						}
					}
					unlink('update.txt');
				}
				$this->ajaxReturn( 1 ,"更新成功,请手动清空缓存！");
			}else{
				$this->ajaxReturn( 0, "请开启支持在线更新相关类：php.ini中 php_zip.dll扩展");
			}
		}else{
            $this->ajaxReturn( 0, $result['msg'] );
        }
    }


	private function _get_tb_top() {
        vendor('Itaoke.TopClient');
        vendor('Itaoke.RequestCheckUtil');
        vendor('Itaoke.Logger');
        $tb_top = new \TopClient;
        $tb_top->appkey = $this->_tbconfig['app_key'];
        $tb_top->secretKey = $this->_tbconfig['app_secret'];
        return $tb_top;
    }


}
?>
